package com.doge.walknovel.api.model

import android.graphics.Typeface
import com.doge.walknovel.business.reader.menus.ReaderHelper
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.utils.PAGE_SIZE

data class ChapterInfo(
    val chapterId: Int,
    val chapterIndex: Int?, // 章节序号，第几章
    val chapterTitle: String?,
    val price: Int?,
    val priceStatus: Int?, // 1是锁住了 0解开了
    val contentType: Int?, // [章节内容类型：1text、2richText、3url.] 4的时候有下载路径
    val chapterContent: String?,
    val preview: String?,
    val nextChapterId: Int?,
    val preChapterId: Int?,
    val appUnlocking: LoadStatus? = null,
    val appUIUpdateFlag: Any? = Any(),
) {
    val appTypeface: Typeface get() = ReaderHelper.typeface
    val locked: Boolean get() = priceStatus == 1
    val free: Boolean get() = (price ?: 0) <= 0
    val filePath: String? get() = if (contentType == 4) chapterContent else null
    val fileName: String? get() = extract(filePath)
    val text: String? get() = if (contentType == 1) chapterContent else null

    companion object {
        val NONE = ChapterInfo(0, 0, null, null, null, null, null, null, null, null)
    }
}

const val chapterPageSize = PAGE_SIZE * 5 // api speed to slow

fun extract(url: String?): String? {
    if (url == null) return null
    val suffixIdx = url.lastIndexOf(".").let { if (it >= 0) it else url.length }
    val preIdx = url.lastIndexOf("/").let { if (it >= 0) it + 1 else return null }
    if (preIdx >= suffixIdx) return null
    return url.substring(preIdx, suffixIdx)
}